<?php
/**
 * Created by PhpStorm.
 * User: chang
 * Date: 2019/4/1
 * Time: 11:54
 */

namespace App\Http\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Gift;

class Counter extends Base
{
    // 礼物类型,goods_type
    const GOOD_TYPE_OF_ONE = 1;// 钻石礼物
    const GOOD_TYPE_OF_TWO = 2;// 小屋礼物
    const GOOD_TYPE_OF_THREE = 3;// 计时礼物
    const GOOD_TYPE_OF_FOUR = 4;// 免费礼物

    protected $primaryKey = "id";
    protected $table = "counter";
    protected $cateTable = "counter_cate";
    protected $counterNewPlay = 'counter_new_play';
    protected $modelGift;
    protected $giftTable = 'gift_library';
    protected $roomTestList = 'ROOMTEST:LIST'; //string ROOMTEST:LIST =>{json} 测试房间

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->modelGift = new Gift();
    }


    /**
     * 获取货柜列表
     * @param $id
     * @return array|mixed
     */
    public function getCounter($id, $cateId = 0){

        $select = DB::table('counter')
            ->where('counter_id', '=', $id);

        if ($cateId) {
            $select = $select->where('counter_cate_id', '=', $cateId);
        }

        $result =  $select->orderBy('order')
            ->get(['goods_type','goods_id','name','explain','exp_icon_url','exp_url','image', 'id', 'end_time', 'start_time']);
        if (empty($result)){
            return [];
        }
        $result = $this->objToArray($result);
        for ($i = 0; $i < count($result); $i++) {
            if (!empty($result[$i]['image'])){
                $result[$i]['image'] = getPicDomain() . $result[$i]['image'];
            }
            if (!empty($result[$i]['exp_icon_url'])){
                $result[$i]['exp_icon_url'] = getPicDomain() . $result[$i]['exp_icon_url'];
            }
            if (!empty($result[$i]['exp_url'])){
                $result[$i]['exp_url'] = getPicDomain() . $result[$i]['exp_url'];
            }
        }
        return $result;
    }

    /**
     * 根据ID获取
     *
     * @param int $counterId ID
     * @return array
     * */
    public function getCounterById($counterId)
    {
        $db = DB::table($this->table);
        $getData = $db->where([$this->primaryKey => $counterId])->first();
        return self::objToArray($getData);
    }

    /**
     * 货架-创建/更新/删除
     *
     * @param array $data 数据
     * @param string $type 操作类型 默认 insert=创建，update=更新
     * @param int $counterId 货架ID
     * @return bool
     * */
    public function CUDCounter($data = [], $type = 'insert', $counterId = 0)
    {
        switch ($type) {
            case 'insert':
                $ret = DB::table($this->table)->insertGetId($data);
                break;
            case 'update':
                $ret = DB::table($this->table)->where([$this->primaryKey => $counterId])->update($data);
                break;
            case 'delete':
                $ret = DB::table($this->table)->where([$this->primaryKey => $counterId])->delete();
                break;
            default:
                $ret = false;
                break;
        }

        return $ret;
    }

    /**
     * 获取货架分类
     *
     * @param int $counterId 货柜ID
     * @return array
     * */
    public function getCounterCate($counterId, $roomId=0)
    {
        $db = DB::table($this->cateTable);
        //获取测试房间列表显示所有礼物标签
        $testRoom = Redis::get($this->roomTestList);
        $where = ["counter_id" => $counterId, "status" => 1];
        if(!empty($testRoom)){
            $roomlist = json_decode($testRoom,true);
            if(in_array($roomId,$roomlist)){
                $where = ["counter_id" => $counterId];
            }
        }
        $getData = $db->where($where)->orderBy("sort", "desc")->get();
        return self::objToArray($getData);
    }

    /**
     * 货架分类 根据ID获取
     *
     * @param int $counterId ID
     * @return array
     * */
    public function getCounterCateById($counterId)
    {
        $db = DB::table($this->cateTable);
        $getData = $db->where(["counter_cate_id" => $counterId])->first();
        return self::objToArray($getData);
    }

    /**
     * 货架分类-创建/更新/删除
     *
     * @param array $data 数据
     * @param string $type 操作类型 默认 insert=创建，update=更新
     * @param int $counterId 货架ID
     * @return bool
     * */
    public function CUDCounterCate($data = [], $type = 'insert', $counterId = 0)
    {
        switch ($type) {
            case 'insert':
                $ret = DB::table($this->cateTable)->insertGetId($data);
                break;
            case 'update':
                $ret = DB::table($this->cateTable)->where(["counter_cate_id" => $counterId])->update($data);
                break;
            case 'delete':
                $ret = DB::table($this->cateTable)->where(["counter_cate_id" => $counterId])->delete();
                break;
            default:
                $ret = false;
                break;
        }

        return $ret;
    }

    /**
     * 货架新增玩法
     * 
     * @param array $data
     * @return integer
     */
    public function insertCounterNewPlay(array $data): int {
        return DB::table($this->counterNewPlay)->insertGetId([
            'counter_id' => $data['counter_id'] ?? 0,
            'play_desc' => $data['play_desc'] ?? '',
            'play_url' => $data['play_url'] ?? '',
            'play_icon' => $data['play_icon'] ?? '',
            'play_bakground_pic' => $data['play_bakground_pic'] ?? '',
            'play_desc_color' => $data['play_desc_color'] ?? '',
            'play_title_color' => $data['play_title_color'] ?? ''
        ]);
    }

    /**
     * 编辑货架玩法
     * 
     * @param integer $id 主键
     * @param array $data
     * @return integer
     */
    public function updateCounterNewPlay(array $data = [], int $id = 0): int {
        return DB::table($this->counterNewPlay)->where('id', $id)->update($data);
    }

    /**
     * 删除货架玩法
     * 
     * @param integer $id 主键
     * @return integer
     */
    public function deleteCounterNewPlay(int $id): int {
        return DB::table($this->counterNewPlay)->delete($id);
    }

    /**
     * 玩法列表
     * 
     * @return array
     */
    public function getCounterNewPlay(): array {
        $res = DB::table($this->counterNewPlay)->where('is_show', 1)->get(['counter_id', 'play_desc', 'play_url', 'play_icon', 'play_bakground_pic', 'play_desc_color', 'play_title_color']);
        $res = self::objToArray($res);
        foreach($res as &$item) {
            $item['play_url'] = 'yyaudio://web?url='.$item['play_url'];
            $item['play_icon'] = build_image($item['play_icon']);
            $item['play_bakground_pic'] = build_image($item['play_bakground_pic']);
            $item['play_desc_color'] = $item['play_desc_color'] ? '#'.$item['play_desc_color'] : '';
            $item['play_title_color'] = $item['play_title_color'] ? '#'.$item['play_title_color']: '';
        }
        unset($item);
        return $res;
    }

    /**
     * 获取所有货架礼物
     * */
    public function getCounterAll()
    {
        $list = DB::table($this->table.' as a')
            ->join($this->giftTable.' as b', 'a.goods_id', '=', 'b.gift_id')
            ->where('a.goods_type', 1)
            ->select(['b.gift_id', 'b.name', 'b.image', 'b.price', 'b.is_show', 'a.counter_cate_id'])
            ->groupBy('b.gift_id')
            ->get();
        return self::objToArray($list);
    }

}